class UserController {
    def groovyClassLoader

    def login = {
        User u = User.findByUsername(params.username)
        if (!u) {
            flash.message = 'The user is not existed.'
            redirect(uri: '/')
        } else if (u.password != params.password) {
            flash.message = 'The password is wrong.'
            redirect(uri: '/')
        } else {
            session.LOGIN_USER = u
            redirect(controller: 'hotel', action: 'index')
        }
    }

    def register = {
    }

    def doRegister = {
        User newUser = new User(params)
        if (newUser.save()) {
            session.LOGIN_USER = newUser
            redirect(controller: 'hotel', action: 'index')
        } else {
            request.user = newUser
            render(view: 'register')
        }
    }

    def resetPassword = {
        User user = User.get(session.LOGIN_USER.id)
        user.properties = params

        if (user.save()) {
            flash.messages = ['user.resetPassword']
            redirect(controller: 'hotel', action: 'index')
        } else {
            render(view: 'resetPassword', model: [user: user])
        }
    }

    def logout = {
        session.LOGIN_USER = null
        redirect(uri: '/')
    }
}